CREATE TRIGGER TGCTHD_01 ON CHITIETHOADON FOR INSERT,UPDATE
AS
BEGIN
	DECLARE @COUNT INT
	DECLARE @MASACH VARCHAR(50)
	DECLARE @SOLUONG INT
	SET @MASACH =(SELECT MASACH FROM inserted)
	SET @COUNT =(SELECT SOLUONG FROM inserted)
	SET @SOLUONG =(SELECT SOLUONG FROM Sach WHERE @MASACH = MaSach)
	IF(@COUNT > @SOLUONG)
	BEGIN
		RAISERROR('SO LUONG NHAP VAO LON HON SO LUONG SACH HIEN CO',16,1)
		ROLLBACK TRAN
		RETURN
	END
	UPDATE Sach 
	SET SoLuong = SoLuong - @COUNT WHERE MaSach = @MASACH
END